import store from '@/store'
// 获取当前世界时间
const getUTCTime = (nowTime) => {
  const time = new Date(nowTime)
  return +time
}

// 获取该聊天对象最后一次显示时间的消息的时间，判断是否显示当前消息的时间
const createTimeMessage = (id, idType, msgObj, ifFront) => {
  const messages = store.state.chatInformation.chatInformation.filter(item => item.id === id && item.id_type === idType)[0] ? store.state.chatInformation.chatInformation.filter(item => item.id === id && item.id_type === idType)[0].messages : []
  const timeMessages = messages.filter(item => item.special && item.type === 'time')
  if (timeMessages.length) {
    if (ifFront ? timeMessages[0].msg_id - msgObj.timestamp > 300000 : msgObj.timestamp - timeMessages[timeMessages.length - 1].timestamp > 300000) {
      store.commit('chatInformation/add_Message', {
        message: {
          special: true,
          type: 'time',
          msg_id: msgObj.timestamp
        },
        id,
        idType,
        ifFront
      })
    }
  } else {
    store.commit('chatInformation/add_Message', {
      message: {
        special: true,
        type: 'time',
        msg_id: msgObj.timestamp
      },
      id,
      idType,
      ifFront
    })
  }
}

// 获取当前时区
const getTimeZone = () => {
  const timeOffset = new Date().getTimezoneOffset()
  let timeZone
  if (timeOffset >= 0) {
    timeZone = '+' + timeOffset / 60
  } else {
    timeZone = timeOffset / 60
  }
  return timeZone
}

export default {
  getUTCTime,
  createTimeMessage,
  getTimeZone
}
